package com.example.the_breakfast_club;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class AdminActivity extends Activity {

	// Pseudo Database which stores usernames and passwords.
		// private PseudoDatabase db;
		
		// Real Database, which should replace the PseudoDatabase.
		private DatabaseHandler db;
		
		private EditText mEmailView;
		private String mEmail;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_admin);

		
		db = DatabaseHandler.getInstance(this);
		//User user = db.getUser(textEmailAddress);
		/*
		User admin = new User("sharif@gmail.com", "password"); // hard code sharif�s email and make up a password 
		db.addUser(admin);
		db.setAdmin(admin.getName());
		*/
		
		// Show the Up button in the action bar.
		setupActionBar();
		
		
		/*
		 * We want getText() and string that names user
		 * Context mEdit   = findViewById(R.id.email_admin).getContext();
	*/
	
		
		mEmailView = (EditText) findViewById(R.id.email_admin);
		mEmail = mEmailView.getText().toString();
		
		
		
		findViewById(R.id.unlock_user_button).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						unlockUser();
					}
				});
		
		findViewById(R.id.lock_user_button).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						lockUser();
					}
				});
		
		findViewById(R.id.remove_user_button).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						deleteUser();
					}
				});
		
		findViewById(R.id.continue_button).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						Intent intent = new Intent(view.getContext(), LoginActivity.class);
				        startActivity(intent);
					}
				});
		
		findViewById(R.id.promote_admin_button).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						setAdmin();
					}
				});
	}

	public void lockUser() {
		mEmail = mEmailView.getText().toString();
		db.lockUser(mEmail);
	}
	public void unlockUser() {
		mEmail = mEmailView.getText().toString();
		db.unlockUser(mEmail);
	}
	public void deleteUser() {
		mEmail = mEmailView.getText().toString();
		db.deleteUser(db.getUser(mEmail));
	}
	public void setAdmin() {
		mEmail = mEmailView.getText().toString();
		db.setAdmin(mEmail);
	}
	
	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.admin, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	//TODO: Heirarchical Parents for every screen (so back button works and we can test) 
	//TODO: continue button
	//TODO: crash proof / fail / error messages

}
